package com.example.papercutting.ui.fragement;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ListAdapter;

import com.example.papercutting.R;
import com.example.papercutting.databinding.FragmentCollectionBinding;

import com.example.papercutting.ui.ShowWork;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CollectionFragment extends Fragment {

    private int[] icon = new int[]{R.drawable.work1,R.drawable.work2,R.drawable.work3,R.drawable.work4,
            R.drawable.work5,R.drawable.work6,R.drawable.work7,R.drawable.work8};
    private FragmentCollectionBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentCollectionBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        GridView projects = getActivity().findViewById(R.id.collection_projects);
        List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
        for (int i = 0;i<icon.length;i++){
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("image",icon[i]);
            data.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(getActivity(),data,R.layout.collection_grid_item, new String[]{"image"}, new int[]{R.id.collection_project});
        projects.setAdapter(adapter);
        projects.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ShowWork.class);
                intent.putExtra("image",icon[position]);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}